package enron;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Arrays;
import java.util.ArrayList;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.JTabbedPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.BevelBorder;
import javax.swing.border.EtchedBorder;
import javax.swing.border.SoftBevelBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import enron.Generator.Generator;
import org.jfree.chart.ChartPanel;
import javax.swing.JComboBox;
import java.awt.CardLayout;
import java.awt.Font;

public class EnergyGridUI extends JFrame {
	private static final long serialVersionUID = 4620740172947352449L;
	EnergyGrid myAgent;
	private SpinnerEditor powerSpinnerEditor;
	private JTable tableAppliances;
	
	/**
	 * For testing
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					JFrame window = new EnergyGridUI(null);
					window.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public EnergyGridUI(EnergyGrid agent) {
		myAgent = agent;
		initGUI();
	}
	
	private void initGUI() {
		this.setBounds(100, 100, 397, 341);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowListener() {
			@Override
			public void windowClosed(WindowEvent arg0) {
				((EnergyGridUI)arg0.getComponent()).myAgent.doDelete();
			}

			@Override
			public void windowActivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowClosing(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeactivated(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowDeiconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowIconified(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void windowOpened(WindowEvent arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		JTabbedPane contentPane = new JTabbedPane();
		this.setContentPane(contentPane);
		{
			JPanel tabNewUser = new JPanel();
			tabNewUser.setName("tabNewUser");
			contentPane.addTab("New User", null, tabNewUser, null);
			tabNewUser.setLayout(new FormLayout(new ColumnSpec[] {
					FormFactory.UNRELATED_GAP_COLSPEC,
					FormFactory.PREF_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.PREF_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					ColumnSpec.decode("pref:grow"),
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.PREF_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					ColumnSpec.decode("17dlu"),
					FormFactory.RELATED_GAP_COLSPEC,
					ColumnSpec.decode("max(23dlu;pref)"),
					FormFactory.RELATED_GAP_COLSPEC,
					FormFactory.DEFAULT_COLSPEC,
					FormFactory.RELATED_GAP_COLSPEC,
					ColumnSpec.decode("22dlu"),
					FormFactory.UNRELATED_GAP_COLSPEC,},
				new RowSpec[] {
					FormFactory.UNRELATED_GAP_ROWSPEC,
					FormFactory.PREF_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.PREF_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.PREF_ROWSPEC,
					FormFactory.RELATED_GAP_ROWSPEC,
					RowSpec.decode("fill:max(101dlu;pref):grow"),
					FormFactory.RELATED_GAP_ROWSPEC,
					FormFactory.PREF_ROWSPEC,
					FormFactory.UNRELATED_GAP_ROWSPEC,}));
			
			{
				JLabel lblName = new JLabel("Name:");
				tabNewUser.add(lblName, "2, 2, right, center");
			}
			
			{
				JTextField textUserName = new JTextField();
				textUserName.setName("textUserName");
				textUserName.setMinimumSize(new Dimension(0, 30));
				tabNewUser.add(textUserName, "4, 2, 13, 1, fill, default");
				textUserName.setColumns(10);
			}
			
			{
				JLabel lblAddress = new JLabel("Address:");
				tabNewUser.add(lblAddress, "2, 4, right, center");
			}
			
			{
				JLabel lblStreet = new JLabel("Street: ");
				tabNewUser.add(lblStreet, "4, 4, right, center");
			}
			
			{
				JTextField textUserStreet = new JTextField();
				textUserStreet.setName("textUserStreet");
				textUserStreet.setMinimumSize(new Dimension(0, 30));
				tabNewUser.add(textUserStreet, "6, 4, 11, 1, fill, default");
				textUserStreet.setColumns(10);
			}
			
			{
				JLabel lblCity = new JLabel("City:");
				tabNewUser.add(lblCity, "4, 6, right, center");
			}
			
			{
				JTextField textUserCity = new JTextField();
				textUserCity.setName("textUserCity");
				textUserCity.setMinimumSize(new Dimension(0, 30));
				tabNewUser.add(textUserCity, "6, 6, fill, default");
				textUserCity.setColumns(10);
			}
			
			{
				JLabel lblState = new JLabel("State:");
				tabNewUser.add(lblState, "8, 6, right, center");
			}
			
			{
				JTextField textUserState = new JTextField();
				textUserState.setName("textUserState");
				textUserState.setPreferredSize(new Dimension(15, 20));
				tabNewUser.add(textUserState, "10, 6, fill, default");
				textUserState.setColumns(3);
			}
			
			{
				JLabel lblPostCode = new JLabel("Post Code:");
				tabNewUser.add(lblPostCode, "12, 6, 3, 1, right, center");
			}
			
			{
				JTextField textUserPostCode = new JTextField();
				textUserPostCode.setName("textUserPostCode");
				textUserPostCode.setMinimumSize(new Dimension(0, 30));
				tabNewUser.add(textUserPostCode, "16, 6");
				textUserPostCode.setColumns(4);
			}
			{
				JPanel panelAppliances = new JPanel();
				panelAppliances.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Appliances", TitledBorder.LEADING, TitledBorder.TOP, null, null));
				tabNewUser.add(panelAppliances, "1, 8, 17, 1, fill, fill");
				GridBagLayout gbl_panelAppliances = new GridBagLayout();
				gbl_panelAppliances.columnWidths = new int[]{0, 22, 0};
				gbl_panelAppliances.rowHeights = new int[]{0, 79, 0};
				gbl_panelAppliances.columnWeights = new double[]{1.0, 0.0, Double.MIN_VALUE};
				gbl_panelAppliances.rowWeights = new double[]{0.0, 1.0, Double.MIN_VALUE};
				panelAppliances.setLayout(gbl_panelAppliances);
				{
					JSlider sliderApplianceCount = new JSlider();
					sliderApplianceCount.setSnapToTicks(true);
					sliderApplianceCount.setName("sliderApplianceCount");
					sliderApplianceCount.setValue(0);
					sliderApplianceCount.setPaintTicks(true);
					sliderApplianceCount.setPaintLabels(true);
					sliderApplianceCount.setMinorTickSpacing(1);
					sliderApplianceCount.setMaximum(50);
					sliderApplianceCount.setMajorTickSpacing(10);
					sliderApplianceCount.addChangeListener(new ChangeListener() {
						
						public void stateChanged(ChangeEvent event) {
							int value = ((JSlider) event.getSource()).getValue();
							ArrayList<Component> components = new ArrayList<Component>(Arrays.asList(((JSlider)event.getSource()).getParent().getComponents()));
							JLabel lblSliderVal = (JLabel) components.get(components.indexOf(event.getSource())+1);
							JTable table = (JTable)((JScrollPane)components.get(components.indexOf(event.getSource())+2)).getViewport().getView();
							lblSliderVal.setText(Integer.toString(value));
							((DefaultTableModel)table.getModel()).setRowCount(value);
						}
					});
					GridBagConstraints gbc_sliderApplianceCount = new GridBagConstraints();
					gbc_sliderApplianceCount.fill = GridBagConstraints.HORIZONTAL;
					gbc_sliderApplianceCount.insets = new Insets(0, 0, 5, 5);
					gbc_sliderApplianceCount.gridx = 0;
					gbc_sliderApplianceCount.gridy = 0;
					panelAppliances.add(sliderApplianceCount, gbc_sliderApplianceCount);
				}
				{
					JLabel lblApplianceCount = new JLabel("0");
					lblApplianceCount.setName("lblApplianceCount");
					lblApplianceCount.setMinimumSize(new Dimension(20, 14));
					GridBagConstraints gbc_lblApplianceCount = new GridBagConstraints();
					gbc_lblApplianceCount.insets = new Insets(0, 0, 5, 0);
					gbc_lblApplianceCount.gridx = 1;
					gbc_lblApplianceCount.gridy = 0;
					panelAppliances.add(lblApplianceCount, gbc_lblApplianceCount);
				}
				{
					JScrollPane scrollPaneAppliances = new JScrollPane();
					scrollPaneAppliances.setName("scrollPaneAppliances");
					scrollPaneAppliances.setBorder(new SoftBevelBorder(BevelBorder.LOWERED, null, null, null, null));
					scrollPaneAppliances.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_ALWAYS);
					scrollPaneAppliances.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
					GridBagConstraints gbc_scrollPaneAppliances = new GridBagConstraints();
					gbc_scrollPaneAppliances.gridwidth = 2;
					gbc_scrollPaneAppliances.insets = new Insets(0, 0, 0, 5);
					gbc_scrollPaneAppliances.fill = GridBagConstraints.BOTH;
					gbc_scrollPaneAppliances.gridx = 0;
					gbc_scrollPaneAppliances.gridy = 1;
					panelAppliances.add(scrollPaneAppliances, gbc_scrollPaneAppliances);
					{
						tableAppliances = new JTable();
						tableAppliances.setName("tableAppliances");
						tableAppliances.setModel(new DefaultTableModel(new String[] {"Name", "Power (W)", "Initial State"},0) {
							private static final long serialVersionUID = 5079746104881300480L;
							Class<?>[] columnTypes = new Class[] {
								String.class, Long.class, Boolean.class
							};
							public Class<?> getColumnClass(int columnIndex) {
								return columnTypes[columnIndex];
							}
						});
						tableAppliances.getColumnModel().getColumn(2).setResizable(false);
						
						tableAppliances.setFillsViewportHeight(true);
						
						tableAppliances.getColumn("Name").setCellEditor(new DefaultCellEditor(new JTextField()));
						tableAppliances.getColumn("Name").setMinWidth(80);
						tableAppliances.getColumn("Name").setPreferredWidth(200);
						
						SpinnerNumberModel cellSpinnerModel = new SpinnerNumberModel(new Long(0L), new Long(0L), new Long(Long.MAX_VALUE), new Long(1L));
						
						powerSpinnerEditor = new SpinnerEditor(cellSpinnerModel, tableAppliances.getDefaultEditor(Long.class));
						tableAppliances.getColumn("Power (W)").setCellEditor(powerSpinnerEditor);
						cellSpinnerModel = new SpinnerNumberModel(new Long(0L), new Long(0L), new Long(Long.MAX_VALUE), new Long(1L));
						//TODO reenable cell renderer
						tableAppliances.getColumn("Power (W)").setCellRenderer(new SpinnerRenderer(cellSpinnerModel,tableAppliances.getDefaultRenderer(Long.class)));
						tableAppliances.getColumn("Power (W)").setMinWidth(80);
						tableAppliances.getColumn("Power (W)").setPreferredWidth(80);
						
						tableAppliances.getColumn("Initial State").setCellRenderer(new CheckBoxRenderer("Turned On",tableAppliances.getDefaultRenderer(Boolean.class), false));
						tableAppliances.getColumn("Initial State").setCellEditor(new DefaultCellEditor(new JCheckBox("Turned On")));
						tableAppliances.getColumn("Initial State").setMaxWidth(90);
						tableAppliances.getColumn("Initial State").setMinWidth(90);

						tableAppliances.setMinimumSize(new Dimension(tableAppliances.getWidth(), 100));
						tableAppliances.setPreferredScrollableViewportSize(tableAppliances.getMinimumSize());
						
						tableAppliances.setDragEnabled(false);
						
						scrollPaneAppliances.setViewportView(tableAppliances);
						//scrollPane.getViewport().setSize(table.getMaximumSize());
						//scrollPane.getViewport().setViewSize(table.getPreferredSize());
					}
				}
			}
			
			{
				JButton btnCreateUser = new JButton("Create");
				btnCreateUser.setName("btnCreateUser");
				tabNewUser.putClientProperty("defaultButton", btnCreateUser);
				btnCreateUser.addActionListener(new CreateGridClientListener());
				tabNewUser.add(btnCreateUser, "10, 10, 3, 1, center, default");
				contentPane.getRootPane().setDefaultButton(btnCreateUser);
			}
			
			{
				JButton btnClearUser = new JButton("Clear");
				btnClearUser.setName("btnClearUser");
				btnClearUser.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						clearComponents(((Component)e.getSource()).getParent().getComponents());
					}
					
					private void clearComponents(Component[] components) {
						TableCellEditor cellEditor = tableAppliances.getCellEditor();
						if(cellEditor != null) {
							cellEditor.cancelCellEditing();
						}
						for(Component component : components) {
							if(JTextField.class.isInstance(component)) {
								//System.out.println("Clearing " + component.getName());
								((JTextField)component).setText("");
							} else if(JSlider.class.isInstance(component)) {
								//System.out.println("Clearing " + component.getName());
								((JSlider)component).setValue(0);
							} else if(JPanel.class.isInstance(component)) {
								clearComponents(((JPanel)component).getComponents());
							} else {
								//System.out.println(component.getClass().getName());
							}
						}
					}
				});
				tabNewUser.add(btnClearUser, "14, 10, 3, 1, center, default");
			}
		}
		
		{
			JPanel tabNewSupplier = new JPanel();
			tabNewSupplier.setName("tabNewSupplier");
			contentPane.addTab("New Supplier", null, tabNewSupplier, null);
			{
				tabNewSupplier.setLayout(new FormLayout(new ColumnSpec[] {
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.PREF_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.PREF_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						ColumnSpec.decode("pref:grow"),
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.PREF_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,
						FormFactory.PREF_COLSPEC,
						FormFactory.RELATED_GAP_COLSPEC,},
					new RowSpec[] {
						FormFactory.UNRELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.RELATED_GAP_ROWSPEC,
						RowSpec.decode("fill:max(101dlu;pref):grow"),
						FormFactory.RELATED_GAP_ROWSPEC,
						FormFactory.PREF_ROWSPEC,
						FormFactory.UNRELATED_GAP_ROWSPEC,}));
				
				{
					JLabel lblName = new JLabel("Name:");
					tabNewSupplier.add(lblName, "2, 2, right, center");
				}
				
				{
					JTextField textUserName = new JTextField();
					textUserName.setName("textUserName");
					textUserName.setMinimumSize(new Dimension(0, 30));
					tabNewSupplier.add(textUserName, "4, 2, 7, 1, fill, default");
					textUserName.setColumns(10);
				}
				
				{
					JLabel lblLocation = new JLabel("Location:");
					lblLocation.setName("lblLocation");
					tabNewSupplier.add(lblLocation, "2, 4, right, center");
				}
				
				{
					JLabel lblLongitude = new JLabel("Longitude: ");
					lblLongitude.setName("lblLongitude");
					tabNewSupplier.add(lblLongitude, "4, 4, right, center");
				}
				
				{
					JTextField textSupplierLongitude = new JTextField();
					textSupplierLongitude.setName("textSupplierLongitude");
					textSupplierLongitude.setMinimumSize(new Dimension(0, 30));
					tabNewSupplier.add(textSupplierLongitude, "6, 4, 5, 1, fill, default");
					textSupplierLongitude.setColumns(10);
				}
				
				{
					JLabel lblLatitude = new JLabel("Latitude:");
					lblLatitude.setName("lblLatitude");
					tabNewSupplier.add(lblLatitude, "4, 6, right, center");
				}
				
				{
					JTextField textLatitude = new JTextField();
					textLatitude.setName("textLatitude");
					textLatitude.setMinimumSize(new Dimension(0, 30));
					tabNewSupplier.add(textLatitude, "6, 6, 5, 1, fill, default");
					textLatitude.setColumns(10);
				}
				{
					JPanel panelGenerators = new JPanel();
					panelGenerators.setFont(new Font("Tahoma", Font.BOLD, 11));
					panelGenerators.setBorder(new TitledBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null), "Generators", TitledBorder.LEADING, TitledBorder.TOP, null, null));
					tabNewSupplier.add(panelGenerators, "1, 8, 11, 1, fill, fill");
					panelGenerators.setLayout(new FormLayout(new ColumnSpec[] {
							FormFactory.RELATED_GAP_COLSPEC,
							FormFactory.PREF_COLSPEC,
							FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
							FormFactory.DEFAULT_COLSPEC,
							FormFactory.RELATED_GAP_COLSPEC,
							ColumnSpec.decode("default:grow"),},
						new RowSpec[] {
							RowSpec.decode("fill:pref"),
							FormFactory.RELATED_GAP_ROWSPEC,
							FormFactory.DEFAULT_ROWSPEC,
							FormFactory.RELATED_GAP_ROWSPEC,
							RowSpec.decode("145px:grow"),}));
					{
						JLabel lblTypeOfGenrator = new JLabel("Type of Genrators: ");
						lblTypeOfGenrator.setName("lblTypeOfGenrator");
						panelGenerators.add(lblTypeOfGenrator, "2, 1, 3, 1, right, center");
					}
					{

						JPanel generatorPropertiesPanel = new JPanel();
						ArrayList<Class<Generator>> generatorList = new ArrayList<Class<Generator>>(EnergyGrid.getGeneratorList().values());
						ArrayList<String> generatorNameList = new ArrayList<String>();
						generatorPropertiesPanel.setName("generatorPropertiesPanel");
						CardLayout generatorPropertiesLayout = new CardLayout(0, 0);
						generatorPropertiesPanel.setLayout(generatorPropertiesLayout);
						generatorPropertiesPanel.add(new JPanel(),"blank");
						generatorNameList.add("Select Generator Type");
						generatorNameList.add("----------------------------");
						for(Class<Generator> generator : generatorList) {
							generatorNameList.add(generator.getSimpleName());
							try {
								JPanel genPropPanel = (JPanel) generator.getMethod("getGeneratorOptionsGUI", new Class<?>[]{}).invoke(null, new Object[]{});
								generatorPropertiesPanel.add(genPropPanel,generator.getSimpleName());
								//System.out.println("Add (" + genPropPanel.getName() + "," + generator.getSimpleName() + ")");
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
						generatorPropertiesLayout.first(generatorPropertiesPanel);
						
						DefaultComboBoxModel generatorListModel = new DefaultComboBoxModel(generatorNameList.toArray());
						JComboBox generatorSelector = new JComboBox(generatorListModel);
						
						generatorPropertiesPanel.putClientProperty("enron.GeneratorSelector", generatorSelector);
						
						generatorSelector.setName("generatorSelector");
						generatorSelector.putClientProperty("generatorPropertiesPanel", generatorPropertiesPanel);
						panelGenerators.add(generatorSelector, "6, 1, left, center");

						generatorSelector.addItemListener(new ItemListener(){
							@Override
							public void itemStateChanged(ItemEvent event) {
								JPanel generatorPropertiesPanel = (JPanel) ((JComponent) event.getSource()).getClientProperty("generatorPropertiesPanel");
								CardLayout cardLayout = (CardLayout)generatorPropertiesPanel .getLayout();
								if(((JComboBox)event.getSource()).getSelectedIndex() > 1) {
									cardLayout.show(generatorPropertiesPanel, (String)event.getItem());
								} else {
									cardLayout.show(generatorPropertiesPanel, "blank");
									cardLayout.first(generatorPropertiesPanel);
								}
							}	
						});
						{
							{
								JLabel lblNumberOfGenerators = new JLabel("Number of Generators:");
								lblNumberOfGenerators.setName("lblNumberOfGenerators");
								panelGenerators.add(lblNumberOfGenerators, "2, 3, right, center");
							}
						}
						JSlider generatorCountSlider = new JSlider();
						generatorCountSlider.setMajorTickSpacing(10);
						generatorCountSlider.setMaximum(50);
						generatorCountSlider.setMinorTickSpacing(2);
						generatorCountSlider.setValue(0);
						generatorCountSlider.setPaintTicks(true);
						generatorCountSlider.setName("generatorCountSlider");
						generatorCountSlider.addChangeListener(new ChangeListener() {
							public void stateChanged(ChangeEvent event) {
								int value = ((JSlider) event.getSource()).getValue();
								ArrayList<Component> components = new ArrayList<Component>(Arrays.asList(((JSlider)event.getSource()).getParent().getComponents()));
								JLabel lblSliderVal = (JLabel) components.get(components.indexOf(event.getSource())-1);
								lblSliderVal.setText(Integer.toString(value));
							}
						});
						{
							JLabel lblGeneratorCount = new JLabel("100");
							lblGeneratorCount.setName("lblGeneratorCount");
							panelGenerators.add(lblGeneratorCount, "4, 3");
						}
						panelGenerators.add(generatorCountSlider, "6, 3, fill, center");
						
						panelGenerators.add(generatorPropertiesPanel, "2, 5, 5, 1, fill, fill");
					}
				}
				
				{
					JButton btnCreateSupplier = new JButton("Create");
					btnCreateSupplier.setName("btnCreateSupplier");
					tabNewSupplier.putClientProperty("defaultButton", btnCreateSupplier);
					btnCreateSupplier.addActionListener(new CreateGridClientListener());
					tabNewSupplier.add(btnCreateSupplier, "8, 10, center, default");
				}
				
				{
					JButton btnClearSupplier = new JButton("Clear");
					btnClearSupplier.setName("btnClearSupplier");
					btnClearSupplier.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							clearComponents(((Component)e.getSource()).getParent().getComponents());
						}
						
						private void clearComponents(Component[] components) {
							for(Component component : components) {
								if(JTextField.class.isInstance(component)) {
									//System.out.println("Clearing " + component.getName());
									((JTextField)component).setText("");
								} else if(JSlider.class.isInstance(component)) {
									//System.out.println("Clearing " + component.getName());
									((JSlider)component).setValue(0);
								} else if(JPanel.class.isInstance(component)) {
									clearComponents(((JPanel)component).getComponents());
								} else {
									//System.out.println(component.getClass().getName());
								}
							}
						}
					});
					tabNewSupplier.add(btnClearSupplier, "10, 10, center, default");
				}
			}
		}
		this.pack();
		this.setMinimumSize(this.getSize());
		this.setVisible(true);

		contentPane.addChangeListener(new ChangeListener(){
			@Override
			public void stateChanged(ChangeEvent event) {
				((JComponent) event.getSource()).getRootPane().setDefaultButton((JButton)((JComponent)((JTabbedPane)event.getSource()).getSelectedComponent()).getClientProperty("defaultButton"));			
			}
		});
		{
			ChartPanel chartTest = new ChartGUITemplate("Test Chart");
			chartTest.setName("chartTest");
			contentPane.addTab("Test Chart", null, chartTest, null);
			//chartTest.getChart().getPlot().get
		}
	}

}
